﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Manufacturing;

namespace NotebookS2.Manufacturing.Dialog
{
    public partial class FND_MO : Form
    {
        public FND_MO()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedDocumentNumber { get; set; }


        void BindGrid()
        {
            ManufacturingOrderManager oManager = new ManufacturingOrderManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = new DataTable();

            switch (cboSearchIndex.SelectedIndex)
            {
                case 0:
                    dt = oManager.BrowseMaterialsByMONumber(txtSearch.Text);
                    break;
                case 1:
                    dt = oManager.BrowseMaterialsByProductCode(txtSearch.Text);
                    break;
                case 2:
                    dt = oManager.BrowseMaterialsByProductName(txtSearch.Text);
                    break;
            }

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        private void FND_MO_Load(object sender, EventArgs e)
        {
            cboSearchIndex.SelectedIndex = 0;   
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            this.SelectedDocumentNumber = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DocNum"]).ToString();
            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BindGrid();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindGrid();
            }
        }
    }
}
